// pages/index/component/category/magic_category.js
import {
  list
} from '../../../../api/navigation'
const util = require('../../../../utils/util.js')
const app = getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    info:{
      type:Object
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    allHeight: '',
    tagIds:[],
    categorys: [],
    pageSize:18
  },
  lifetimes: {
    attached() {
      this.data.pageSize = this.data.info.rows
      console.log(this.data.info)
      if (this.data.info.tagids != null) {
        this.data.tagIds = this.data.info.tagids.split(',')
        for (var i = 0; i < this.data.tagIds.length; i++) {
          this.data.tagIds[i] = parseInt(this.data.tagIds[i])
        }
      }
        this.setData({
          tagIds:this.data.tagIds
        })
      this.loadList()
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    toSearch(){
      wx.navigateTo({
        url: '/pages/goods/search/index'
      })
    },
    toRecommend(e) {
      console.log(e)
      if (e.currentTarget.dataset.id.type == 1) {
        wx.navigateTo({
          url: '/pages/goods/category/category?id=' + e.currentTarget.dataset.id.typeId
        })
      }
      if (e.currentTarget.dataset.id.type == 2) {
        wx.navigateTo({
          url: '/pages/index/wxPage/wxPage?id=' + e.currentTarget.dataset.id.typeId
        })
      }
      if (e.currentTarget.dataset.id.type == 3) {
        wx.navigateTo({
          url: '/pages/goods/product/details?id=' + e.currentTarget.dataset.id.typeId,
        })
      }
      if (e.currentTarget.dataset.id.type == 12) {
        window.location.href = e.currentTarget.dataset.id.url
      }
    },
    loadList() {

      list({
        mchId:app.globalData.mchId,
        tagIds:this.data.tagIds,
        pageSize: 18,
      }).then(res => {
        let len = res.data.data.length;
        let n = 9; //假设每行显示4个
        let lineNum = len % 9 === 0 ? len / 9 : Math.floor((len / 9) + 1);
        let array = [];
        for (let i = 0; i < lineNum; i++) {
          // slice() 方法返回一个从开始到结束（不包括结束）选择的数组的一部分浅拷贝到一个新数组对象。且原始数组不会被修改。
          let temp = res.data.data.slice(i * n, i * n + n);
          array.push(temp);
        }
        if (res.data.data.length < 4) {
          this.data.allHeight = '230rpx'
        } else if (res.data.data.length > 3 && res.data.data.length < 7) {
          this.data.allHeight = '460rpx'
        } else if (res.data.data.length > 6) {
          this.data.allHeight = '680rpx'
        }
        this.data.categorys = array
        console.log(this.data.categorys)
        var objForm = {
          logo:''
        }
        var objFormTwo = {
          logo:''
        }
        this.data.categorys.forEach(function(item){
          if(item.length == 1 || item.length == 4 || item.length == 7){
            item.push(objForm)
            item.push(objFormTwo)
          }else if(item.length == 2 || item.length == 5 || item.length == 8){
            item.push(objForm)
          }
        })
        this.setData({
          allHeight: this.data.allHeight,
          categorys: this.data.categorys
        })
      })
    },
  }
})